package com.hi.hiparty.util;

import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.hi.hiparty.entity.Party;
import com.hi.hiparty.entity.Post;
import com.hi.hiparty.ui.session.UserSessionManager;

public class LocationUtil {
	public static final String TAG = LocationUtil.class.getSimpleName();
	
	private static final double EARTH_RADIUS = 6378.137;

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}
	
	public static double[] getCenterLocation(double lat1, double lng1, double lat2, double lng2){
		return new double[]{(lat1+lat2)/2,(lng1+lng2)/2};
	}
	
	public static double getDistance(double lat1, double lng1, double lat2, double lng2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = new BigDecimal(s).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
		return s;
	}

	public static void showUserLocation(Activity activity){
		Uri uri = Uri.parse("geo:"+UserSessionManager.getUserSession(activity).getLatlng()+"?z=15");   
		Intent it = new Intent(Intent.ACTION_VIEW, uri);
		activity.startActivity(it);
	}
	
	public static void showUser2UserRouter(Activity activity,Post post){
		Intent it = null;
		try{
			String[] us = UserSessionManager.getUserSession(activity)
					.getLatlng().split(",");
			double userLat = Double.valueOf(us[0]);
			double userLng = Double.valueOf(us[1]);
			String[] ps = post.getLonlat().split(",");
			double partyLat = Double.valueOf(ps[0]);
			double partyLng = Double.valueOf(ps[1]);

			double cLat = (userLat + partyLat) / 2;
			double cLng = (userLng + partyLng) / 2;

			double d = LocationUtil.getDistance(userLat, userLng, partyLat,
					partyLng);
			int zoom = (d < 10 && d > 4) ? 13 : (d <= 4 && d > 1) ? 14
					: (d <= 1) ? 15 : 12;
			StringBuilder url = new StringBuilder();
			url.append("http://ditu.aliyun.com/#v=").append(cLat).append("~")
					.append(cLng).append("_").append(zoom).append("&tr=b&brv=")
					.append("我的位置|").append(userLat).append("~")
					.append(userLng).append("_").append(post.getUserid()+"|")
					.append(partyLat).append("~").append(partyLng)
					.append("&br=br&brvn=0_0");
			Uri uri = Uri.parse(url.toString());
			it = new Intent(Intent.ACTION_VIEW, uri);
		}catch(Exception e){
			Log.v(TAG, e.toString());
			return;
		}
		activity.startActivity(it);
	}
	
	/**
	 * 基于aliyun的地图搜索url拼接。
	 * @param activity
	 * @param mParty
	 */
	public static void showUser2PartyRouter(Activity activity,Party mParty){
		Intent it = null;
		try{
			String[] us = UserSessionManager.getUserSession(activity)
					.getLatlng().split(",");
			double userLat = Double.valueOf(us[0]);
			double userLng = Double.valueOf(us[1]);
			String[] ps = mParty.getPartyPos().split(",");
			double partyLat = Double.valueOf(ps[0]);
			double partyLng = Double.valueOf(ps[1]);

			double cLat = (userLat + partyLat) / 2;
			double cLng = (userLng + partyLng) / 2;

			double d = LocationUtil.getDistance(userLat, userLng, partyLat,
					partyLng);
			int zoom = (d < 10 && d > 4) ? 13 : (d <= 4 && d > 1) ? 14
					: (d <= 1) ? 15 : 12;
			StringBuilder url = new StringBuilder();
			url.append("http://ditu.aliyun.com/#v=").append(cLat).append("~")
					.append(cLng).append("_").append(zoom).append("&tr=b&brv=")
					.append("我的位置|").append(userLat).append("~")
					.append(userLng).append("_").append("活动地点|")
					.append(partyLat).append("~").append(partyLng)
					.append("&br=br&brvn=0_0");
			Uri uri = Uri.parse(url.toString());
			it = new Intent(Intent.ACTION_VIEW, uri);
		}catch(Exception e){
			Log.v(TAG, e.toString());
			return;
		}
		activity.startActivity(it);
	}
	
	public static void showUser2PartyRouterForGoogle(Activity activity,Party mParty){
		Intent intent = new Intent();
		intent.setAction(android.content.Intent.ACTION_VIEW);
		/* 传入路径规划所需要的地标地址 */
		intent.setData(Uri.parse("http://maps.google.com/maps?f=d&saddr="
				+ UserSessionManager.getUserSession(activity).getLatlng()
				+ "&daddr=" + mParty.getPartyPos() + "&hl=cn"));
		activity.startActivity(intent);
       } 
}
